<template>
	<view class="mine-page page">
		<CustomNav title="我要记录" :isBack="true"></CustomNav>
		<view class="info-block">
			<view class="info-list">
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">记录</view>
					</view>
					<view class="form-content">
						<input placeholder="请输入记录内容..." v-model="form.content" placeholder-style="color:#000;"
							maxlength="300" class="form-crl" />
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">上传图片/视频({{form.pic.length+form.video.length}}/9)</view>
					</view>
					<view class="upload">
						<view v-for="(item,index) in form.pic" class="image-item">
							<image mode="aspectFit" :src="item" @click="previewImg(item)" class="img">
							</image>
							<view @click="closeImg(index,1)" style="display: inline;">
								<view class="iconfont">
									<image src="/static/image/delete.pn" class="delete"></image>
								</view>
							</view>
						</view>
						<view v-for="(item,index) in form.video" class="image-item">
							<video id="myVideo" class="img" style="width: 190rpx;height: 200rpx;border-radius: 20rpx;"
								:src="item" @error="videoErrorCallback" show-fullscreen-btn></video>
							<view @click="closeImg(index,2)" style="display: inline;">
								<view class="iconfont">
									<image src="/static/image/delete.png" class="delete"></image>
								</view>
							</view>
						</view>
						<view class="upload-content" v-if="(form.pic.length+form.video.length)<9" @click="chooseMedia">
							<image src="/static/image/upload.png" class="upload"></image>
						</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">位置</view>
					</view>
					<view class="form-content" @click="chooseLocation">
						<input v-model="form.position" :disabled="true" class="form-crl1"
							placeholder-style="color:#000;" placeholder-class="input-placeholder" placeholder="请选择位置" />
						<view class="right">
							<view class="iconfont icon-gengduo"></view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="btn-item">
			<view class="btn">
				<view class="btn normal" @click="submit">保存</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		validate
	} from '@/util/validate.js'
	import {
		getCurrentTime
	} from '@/util/util.js'
	import {
		addAndUpdateRecord
	} from '@/api/animal.js'
	import config from '@/config.js';
	import {
		getStorage,
		showToast
	} from '@/util/auth';
	export default {
		data() {
			return {
				form: {
					content: '',
					pic: [],
					video: [],
					multiMedia: '',
					position: '',
					latitude: '',
					longitude: ''
				},
				rules: {
					content: {
						name: '记录',
						required: true
					},
					multiMedia: {
						name: '图片或视频',
						required: true,
						validator: function(value, form, callback) {
							if (value == 0) {
								callback('请上传图片或视频')
							}
							callback();
						}
					},
					position: {
						name: '位置',
						required: true,
						validator: function(value, form, callback) {
							if (value == 0) {
								callback('请选择位置')
							}
							callback();
						}
					}
				}
			}
		},
		onLoad() {},
		methods: {
			// 上传视频和图片
			chooseMedia(e) {
				let token = getStorage('token');
				uni.chooseMedia({
					maxDuration: 60,
					count: 1,
					mediaType: ['image', 'video'],
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						if (res.length < 2) { // 小于2则没有选择图片
							uni.hideLoading()
							return
						}
						let filePath = res.tempFiles[0].tempFilePath;
						let fileType = res.tempFiles[0].type;
						let that1 = this;
						uni.showLoading({
							title: '上传中',
							mask: true
						})
						uni.uploadFile({
							url: config.requestUrl + '/sbbz/file/uploadApi', //post请求的地址
							filePath: filePath,
							name: 'file',
							fileType: fileType,
							header: {
								'authori-zation': token
							},
							success: (myres) => {
								uni.hideLoading()
								const data = JSON.parse(myres.data);
								if (data.code == 200) {
									if (data.data.fileType == 1) {
										this.form.pic.push(data.data.url)
									} else if (data.data.fileType == 2) {
										this.form.video.push(data.data.url)
									}
								} else {
									uni.hideLoading()
									showToast('上传失败');
								}
							}
						})
						// })
					}
				})
			},
			// 删除图片
			closeImg(index, e) {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '是否删除当前文件',
					success: function(res) {
						if (res.confirm) {
							if (e == 1) {
								that.form.pic.splice(index, 1);
							} else if (e == 2) {
								that.form.video.splice(index, 1);
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//选择位置
			chooseLocation() {
				uni.chooseLocation({
					latitude: this.$store.getters.position.latitude,
					longitude: this.$store.getters.position.longitude,
					success: (e) => {
						this.form.position = e.address;
						this.form.latitude = e.latitude;
						this.form.longitude = e.longitude;
					},
				})
			},
			submit() {
				let form = JSON.parse(JSON.stringify(this.form));
				if (form.pic.length > 0 && form.video.length > 0) {
					form.multiMedia = form.pic.join(',') + ',' + form.video.join(',')
				} else if (form.pic.length > 0 && form.video.length == 0) {
					form.multiMedia = form.pic.join(',')
				} else {
					form.multiMedia = form.video.join(',')
				}
				if (validate(form, this.rules)) {
					addAndUpdateRecord(form).then(res => {
						uni.showToast({
							title: '提交成功',
							icon: 'none',
							duration: 1000,
							mask: true,
							success: function() {
								setTimeout(function() {
									uni.navigateBack();
								}, 1000)
							},
						})
					}).finally(() => {
						uni.stopPullDownRefresh();
					})
				}
			}
		},
		onShow() {

		}
	}
</script>

<style lang="scss">
	.mine-page {
		overflow: hidden;

		.info-block {
			margin-top: 26%;
			padding: 30rpx;
			background-color: #F7F6F9;

			.info-list {
				background-color: #fff;
				border-radius: 20rpx;
				padding: 30rpx;
				margin-bottom: 20rpx;

				.form-item:last-child {
					border-bottom: none;
				}

				.form-item {
					display: flex;
					flex-direction: column;
					border-bottom: 1px solid #F7F7F7;
					padding-bottom: 30rpx;
					margin-top: 30rpx;

					.name-item {
						display: flex;
						align-items: center;
						margin-bottom: 30rpx;

						.must {
							color: #EA0000;
						}

						.name {
							font-size: 30rpx;
						}
					}

					.form-content {
						display: flex;
						align-items: center;
						justify-content: space-between;
						width: 100%;

						.form-crl1 {
							width: calc(100% - 60rpx);
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
						}

						.form-crl {
							width: 100%;
						}

						.right {
							display: flex;
							align-items: center;

							.iconfont {
								color: #CDCDCD;
								font-size: 32rpx;
							}

							.distance {
								color: #767676;
								font-size: 28rpx;
							}
						}
					}

					.upload {
						display: flex;
						flex-direction: row;
						flex-wrap: wrap;

						.image-item {
							margin-top: 20rpx;
							margin-right: 20rpx;
							display: flex;
							flex-direction: row;
							// box-sizing: border-box;
							// border: 1px solid #efefef;

							.img {
								width: 190rpx;
								height: 200rpx;
								border-radius: 20rpx;
							}

							.iconfont {
								.delete {
									width: 20px;
									height: 20px;
									position: absolute;
									margin-left: -30rpx;
								}
							}
						}

						.image-item:nth-child(3n) {
							margin-right: 0;
						}

						.delete-item {
							width: 100%;
							display: flex;
							justify-content: flex-end;

							.delete {
								width: 20px;
								height: 20px;
								position: absolute;
							}
						}

						.image-item1 {
							display: flex;
							flex-direction: row;

							.delete {
								margin-top: 10rpx;
								margin-left: -40rpx;
								width: 20px;
								height: 20px;
								position: absolute;
							}
						}

						.upload-content {
							position: relative;
							background: #fafafa;
							border-radius: 5px;
							width: 200rpx;
							height: 200rpx;
							display: flex;
							align-items: center;
							justify-content: center;
							margin-top: 20rpx;

							.title {
								position: absolute;
								top: 50%;
								left: 50%;
							}

							.upload {
								width: 90rpx;
								height: 90rpx;
							}
						}
					}
				}
			}
		}

		.btn-item {
			display: flex;
			justify-content: center;
			margin-bottom: 40rpx;
		}
	}
</style>